package com.plian.system.service.fr.indicatorswarning;

import com.plian.Tools.StringUtil;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.fr.indicatorswarning.IndicatorsWarningDao;
import com.plian.system.dao.sys.orgzation.OrgzationDao;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.page.Page;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class IndicatorsWarningService {
    @Autowired
    IndicatorsWarningDao indicatorsWarningDao;
    @Autowired
    private OrgzationDao orgzationDao;
    private static final Logger LOGGER = LoggerFactory.getLogger(IndicatorsWarningService.class);

    /*
     * 查询
     * */
    public ListResult<PageData> listPage(PageData pd) {
        try {
            String orgId = TokenUtil.getTokenOrgId();
            pd.put("orgId",orgId);
            //String orgId = pd.getString("orgId");
            if (StringUtil.isNotBlank(orgId)){
                PageData org = orgzationDao.findbyid(orgId);
                if (org != null && "0".equals(org.getString("parentId"))) {
                    //gzw时查所有
                    pd.remove("orgId");
                }

            }
            Page page = new Page(true, pd);
            List<PageData> list = indicatorsWarningDao.listPage(page);
            ListResult<PageData> Result = new ListResult<>(CommonCode.SUCCESS);
            Result.setTotal(page.getTotal());
            Result.setList(list);
            return Result;
        } catch (Exception e) {
             LOGGER.error(ExceptionUtils.getStackTrace(e));
            return new ListResult<>(CommonCode.ERROR);
        }
    }
//    /* TODO DEL
//     * 查询
//     * */
//    public ListResult<PageData> listMonthPage(PageData pd) {
//        try {
//            String orgId = pd.getString("orgId");
//
//            if (StringUtil.isNotBlank(orgId)){
//                PageData org = orgzationDao.findbyid(orgId);
//                if (org != null && "0".equals(org.getString("parentId"))) {
//                    pd.remove("orgId");
//                }
//            }
//
//            Page page = new Page(true, pd);
//            List<PageData> list = indicatorsWarningDao.listMonthPage(page);
//            ListResult<PageData> Result = new ListResult<>(CommonCode.SUCCESS);
//            Result.setTotal(page.getTotal());
//            Result.setList(list);
//            return Result;
//        } catch (Exception e) {
//             LOGGER.error(ExceptionUtils.getStackTrace(e));
//            return new ListResult<>(CommonCode.ERROR);
//        }
//    }
    /*
     * 更新当月数据
     * */
    public ObjResult update(PageData pd) {
        ObjResult objResult;
        try {
            indicatorsWarningDao.update(pd);
            objResult = new ObjResult(CommonCode.SUCCESS);
            return objResult;
        } catch (Exception e) {
             LOGGER.error(ExceptionUtils.getStackTrace(e));
            return new ObjResult(CommonCode.ERROR);
        }
    }

    /*
     * 加进当月数据
     * */
    public ObjResult insertOrUpdateWarn(PageData pd) {
        ObjResult objResult;
        try {
            PageData pageData = indicatorsWarningDao.selectWarn(pd);
            if (pageData == null){
                //新增
                //pd.put("createTime",(Date)pd.get("time"));
                indicatorsWarningDao.insertWarn(pd);
            }else {
                //修改
                pageData.put("update_time", pd.get("createTime"));
                pageData.put("warning_data",pd.get("warningData"));
                indicatorsWarningDao.updateWarn(pageData);
            }
            objResult = new ObjResult(CommonCode.SUCCESS);
            return objResult;
        } catch (Exception e) {
            LOGGER.error(ExceptionUtils.getStackTrace(e));
            return new ObjResult(CommonCode.ERROR);
        }
    }

}
